package com.hupu.jrs.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.hupu.jrs.R
import com.hupu.jrs.injector.HasComponent
import com.hupu.jrs.util.ResourceUtil
import com.hupu.jrs.widget.ProgressBarCircularIndeterminate
import com.hupu.jrs.widget.ProgressFragment

/**
 * Created by sll on 2016/3/9.
 */
abstract class BaseFragment : ProgressFragment() {

    private var tvError: TextView? = null
    private var tvEmpty: TextView? = null
    private var tvLoading: TextView? = null
    private var btnReload: Button? = null

    abstract fun initInjector()

    abstract fun initContentView(): Int

    /**
     * 得到Activity传进来的值
     */
    abstract fun getBundle(bundle: Bundle)

    /**
     * 初始化控件
     */
    abstract fun initUI(view: View)

    /**
     * 在监听器之前把数据准备好
     */
    abstract fun initData()

    override fun onViewCreated(view: View, savedInstanceState: Bundle) {
        initInjector()
        getBundle(getArguments())
        initUI(view)
        initData()
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onCreateContentView(inflater: LayoutInflater): View {
        return inflater.inflate(initContentView(), null)
    }

    override fun onCreateContentErrorView(inflater: LayoutInflater): View {
        val error = inflater.inflate(R.layout.error_view_layout, null)
        tvError = error.findViewById(R.id.tvError)
        error.findViewById<View>(R.id.btnReload).setOnClickListener(View.OnClickListener { onReloadClicked() })
        return error
    }

    override fun onCreateContentEmptyView(inflater: LayoutInflater): View {
        val empty = inflater.inflate(R.layout.empty_view_layout, null)
        tvEmpty = empty.findViewById(R.id.tvEmpty)
        btnReload = empty.findViewById(R.id.btnReload)
        empty.findViewById<View>(R.id.btnReload).setOnClickListener(View.OnClickListener { onReloadClicked() })
        return empty
    }

    override fun onCreateProgressView(inflater: LayoutInflater): View {
        val loading = inflater.inflate(R.layout.loading_view_layout, null)
        tvLoading = loading.findViewById(R.id.tvLoading)
        val progressBar = loading.findViewById<View>(R.id.progress_view) as ProgressBarCircularIndeterminate
        progressBar.setBackgroundColor(ResourceUtil.getThemeColor(getActivity()))
        return loading
    }

    fun setErrorText(text: String) {
        tvError!!.text = text
    }

    fun setErrorText(textResId: Int) {
        setErrorText(getString(textResId))
    }

    fun setEmptyText(text: String) {
        tvEmpty!!.text = text
    }

    fun setEmptyButtonVisible(visible: Int) {
        btnReload!!.visibility = visible
    }

    fun setEmptyText(textResId: Int) {
        setEmptyText(getString(textResId))
    }

    fun setLoadingText(text: String) {
        tvLoading!!.text = text
    }

    fun setLoadingText(textResId: Int) {
        setLoadingText(getString(textResId))
    }

    //Override this to reload
    open fun onReloadClicked() {

    }

    protected fun <C> getComponent(componentType: Class<C>): C {
        return componentType.cast((getActivity() as HasComponent<C>).component)
    }
}
